package io.renren.modules.bs.controller;

import cn.hutool.core.bean.BeanUtil;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.bs.entity.BsDepartmentAdministratorEntity;
import io.renren.modules.bs.entity.BsDepartmentEntity;
import io.renren.modules.bs.service.BsDepartmentAdministratorService;
import io.renren.modules.bs.service.BsDepartmentService;
import io.renren.modules.bs.service.BsHospitalService;
import io.renren.modules.bs.vo.BsDepartmentAdministratorEntityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-08-15 23:03:15
 */
@RestController
@RequestMapping("bs/departmentAdministrator")
public class BsDepartmentAdministratorController {

    @Autowired
    private BsDepartmentAdministratorService bsDepartmentAdministratorService;

    @Autowired
    private BsHospitalService hospitalService;

    @Autowired
    private BsDepartmentService departmentService;

    /**
     * 列表
     */
    @RequestMapping("/list")

    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bsDepartmentAdministratorService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{departmentAdministratorId}")
    public R info(@PathVariable("departmentAdministratorId") Long departmentAdministratorId){

        BsDepartmentAdministratorEntityVo bsDepartmentAdministratorEntityVo = new BsDepartmentAdministratorEntityVo();
        BsDepartmentAdministratorEntity bsDepartmentAdministrator = bsDepartmentAdministratorService.getById(departmentAdministratorId);
        BsDepartmentEntity departmentEntity = this.departmentService.getById(bsDepartmentAdministrator.getDepartmentId());

        bsDepartmentAdministrator.setAdministratorPwd("");
        BeanUtil.copyProperties(bsDepartmentAdministrator,bsDepartmentAdministratorEntityVo);
        bsDepartmentAdministratorEntityVo.setHospitalId(departmentEntity.getHospitalId());
        return R.ok().put("bsDepartmentAdministrator", bsDepartmentAdministratorEntityVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")

    public R save(@RequestBody BsDepartmentAdministratorEntity bsDepartmentAdministrator){

        try {
            bsDepartmentAdministratorService.saveModel(bsDepartmentAdministrator);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")

    public R update(@RequestBody BsDepartmentAdministratorEntity bsDepartmentAdministrator){
		bsDepartmentAdministratorService.updateModel(bsDepartmentAdministrator);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")

    public R delete(@RequestBody Long[] departmentAdministratorIds){
		bsDepartmentAdministratorService.removeByIds(Arrays.asList(departmentAdministratorIds));

        return R.ok();
    }

}
